#!/bin/sh

if cat $HOME/.profile | grep xtensa-esp32-elf; then
   echo "esp toolchain is done!"
else
   echo "download ESP32 toolchain and set to path:\n"
   wget https://dl.espressif.com/dl/xtensa-esp32-elf-linux64-1.22.0-80-g6c4433a-5.2.0.tar.gz
   chmod +x xtensa-esp32-elf-linux64-1.22.0-80-g6c4433a-5.2.0.tar.gz
   sudo tar -xzf xtensa-esp32-elf-linux64-1.22.0-80-g6c4433a-5.2.0.tar.gz -C /opt/

   echo "#ESP build toolchain" >> ~/.profile
   echo "ESP_TOOL=/opt/xtensa-esp32-elf" >> ~/.profile
   echo "alias get_esp32='export PATH=\"\${ESP_TOOL}/bin:\$PATH\"'" >> ~/.profile
fi

sudo apt-get install git make gcc libncurses-dev flex bison gperf python python-click python-pip python-setuptools python-serial python-cryptography python-future python-pyparsing python-pyelftools cmake ninja-build ccache

#git clone https://gitee.com/ClarePhang/esp-adf.git
git clone https://gitee.com/uxiaodou/esp-adf.git
cd esp-adf
git checkout -b v1.0
sed -i 's/github.com\/espressif/gitee.com\/uxiaodou/' .gitmodules
git submodule update --init

adf_dir=$PWD
echo "\033[33m esp-adf dir is: $adf_dir \033[0m"

git submodule sync
#sed -r 's#/github.com\/.*([\/])#\1gitee.com\/ClarePhang\/#' esp-idf/.gitmodules
sed -i -E 's#/github.com\/.*([\/])#\1gitee.com\/uxiaodou\/#' esp-idf/.gitmodules
cd esp-idf
git submodule update --init
git submodule sync

idf_dir=$PWD
echo "\033[33m esp-idf dir is: $idf_dir \033[0m"

if cat $HOME/.profile | grep IDF_PATH; then
   echo "IDF_PATH is setted!"
else
   echo "export ADF_PATH=$adf_dir" >> $HOME/.profile
   echo "export IDF_PATH=$idf_dir" >> $HOME/.profile
fi

